# -*-coding:utf-8 -*-
from django.http import HttpResponseRedirect
from xadmin.sites import site
from xadmin import views
from xadmin.views.base import *
from django.conf import settings
from django.apps import apps
from datetime import date, timedelta
import pip

class AboutView(CommAdminView):
    title = u"关于"
    template = 'xadmin/core/about.html'
    def get_install_modules_ver(self):
        pass

    def get_registry_apps(self):
        self.get_install_modules_ver()
        _apps = []
        today = date.today()
        delta = timedelta(days=30)
        for app in apps.get_app_configs():
            if app.name == 'django.contrib.auth':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-lock'
                app.verbose_name = u'系统安全管理'
                app.description = u'角色、权限、用户管理'
            elif app.name == 'django.contrib.contenttypes':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'系统类型'
                app.description = u'系统内部功能'
            elif app.name == 'django.contrib.sessions':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'会话管理'
                app.description = u'与安全相关，系统内部功能'
            elif app.name == 'django.contrib.sites':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'站点管理'
                app.description = u'与安全相关，系统内部功能'
            elif app.name == 'django.contrib.messages':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'消息管理'
                app.description = u'系统内部功能'
            elif app.name == 'django.contrib.staticfiles':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'静态文件管理'
                app.description = u'系统内部功能'
            elif app.name == 'xadmin':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'基础管理'
                app.description = u'系统内部功能'
            elif app.name == 'reversion':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'数据版本'
                app.description = u'系统内部功能'
            elif app.name == 'easy_thumbnails':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'图像处理'
                app.description = u'系统内部基础功能'
            elif app.name == 'crispy_forms':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-circle-o'
                app.verbose_name = u'表单处理'
                app.description = u'系统内部基础功能'
            elif app.name == 'django.contrib.gis':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-location-arrow '
                app.verbose_name = u'GIS支持库'
                app.description = u'系统内部基础功能'
            elif app.name == 'django.contrib.admin':
                app.order = 10
                app.version = '1.0'
                app.last_update = date(year=2015, month=12, day=23)
                app.icon = 'fa fa-location-arrow'
                app.verbose_name = u'GIS'
                app.description = u'系统内部基础功能'
            else:
                if not hasattr(app, 'order'):
                    app.order = 10
                if not hasattr(app, 'version'):
                    app.version = '-'
                if not hasattr(app, 'last_update'):
                    app.last_update = date(year=2015, month=12, day=23)
                if not hasattr(app, 'icon'):
                    app.icon = 'fa fa-cog'
                if not hasattr(app, 'verbose_name'):
                    app.verbose_name = app.name
                if not hasattr(app, 'description'):
                    app.description = u'系统内部基础支撑功能'
            app.isnew = False
            if today - app.last_update <= delta:
                app.isnew = True
            _apps.append(app)
        return _apps

    @views.filter_hook
    def get_context(self):
        self.get_registry_apps()
        context = super(AboutView, self).get_context()
        breadcrumb = self.get_breadcrumb()
        breadcrumb.append({
            'url': '',
            'title': self.title,
        })
        new_context = {
            'full_title': getattr(settings, "FULL_TITLE"),
            'admin_version': getattr(settings, "VERSION"),
            'admin_title': getattr(settings, "XADMIN_TITLE"),
            'pub_site_title': getattr(settings, "PUB_SITE_TITLE"),
            'licensed_to': getattr(settings, 'LICENSED_TO', ''),
            'title': self.title,
            'icon': '',
            'breadcrumbs': breadcrumb,
            'apps': self.get_registry_apps()
        }
        context.update(new_context)
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context()
        return self.template_response(self.template, context)


site.register_view(r'^about/$', AboutView, name='about')
